package com.czy.owner.adapter;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.czy.owner.R;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.StoreBalanceModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.utils.JsonUtil;
import com.czy.owner.ui.accountbalance.RechargeActivity;
import com.czy.owner.ui.accountbalance.TransactionRecordActivity;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.easyrecycleview.adapter.BaseViewHolder;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by yang on 2017/10/11.
 */

public class StoreBalanceAdapter extends RecyclerArrayAdapter<StoreBalanceModel> {
    private Context context;

    public StoreBalanceAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new StoreBalanceViewHolder(parent);
    }

    class StoreBalanceViewHolder extends BaseViewHolder<StoreBalanceModel>{
        private TextView tv_store_balance;
        private TextView tv_store_name;
        private TextView tv_transaction_record;
        private TextView tv_recharge;
        public StoreBalanceViewHolder(ViewGroup parent) {
            super(parent,R.layout.item_store_balance);
            tv_store_balance = $(R.id.tv_store_balance);
            tv_store_name = $(R.id.tv_store_name);
            tv_transaction_record = $(R.id.tv_transaction_record);
            tv_recharge = $(R.id.tv_recharge);
        }

        @Override
        public void setData(final StoreBalanceModel data) {
            super.setData(data);
            tv_store_balance.setText(String.format("%.2f",data.getStoreBalance()));
            tv_store_name.setText(data.getStoreName());
            tv_transaction_record.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent ti = new Intent(context, TransactionRecordActivity.class);
                    ti.putExtra("storeId",data.getStoreId());
                    context.startActivity(ti);
                }
            });


            tv_recharge.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    isRechargeSwitch(data.getStoreId());
                }
            });
        }
    }
    private void isRechargeSwitch(final  int storeId) {
        String url = Constants.APP_API_BASE + Constants.OWN_MARKETING_GETTOPUPPLANCUSTOMSWITHSETTING;
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("session", UserHelper.getInstance().getSession(context));
        params.addBodyParameter("storeId", SharedPreferencesUtils.getParam(context, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                MyLog.e("yang", "onSuccess==" + result);
                String flag = JsonUtil.getJsonValuesString(result, "flag");
                String data = JsonUtil.getJsonValuesString(result, "data");
                String exp = JsonUtil.getJsonValuesString(result, "exp");
                if (flag.equals("true")) {
                    Intent ti = new Intent(context, RechargeActivity.class);
                    ti.putExtra("storeId",storeId);
                    ti.putExtra("customSwitch",data);
                    context.startActivity(ti);
                } else {
                    PhoneUtils.ShowToastMessage(context, exp);
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                MyLog.e("yang", "onError==" + ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }
}
